package pandabay.web;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import pandabay.domain.Product;
import pandabay.service.ProductManager;
import pandabay.service.SimpleProductManager;
import pandabay.DAO.ProductDAO;

public class NewProductFormController extends SimpleFormController {

    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());

    private ProductManager productManager;
    private SimpleProductManager simpleProductManager;
    
    @Override
	protected ModelAndView onSubmit(HttpServletRequest request,
		HttpServletResponse response, Object command, BindException errors)
		throws Exception {
 
		Product product = (Product)command;
		return new ModelAndView("successNewProduct","product",product);
	}
/*
    public ModelAndView onSubmit(Object command)
            throws ServletException {

        String increase = ((NewProduct) command).getName();
        logger.info("Increasing prices by " + increase + "%.");

        //productManager.increasePrice(increase);

        logger.info("returning from NewProductForm view to " + getSuccessView());

        return new ModelAndView(new RedirectView(getSuccessView()));
    }


    protected Object formBackingObject(HttpServletRequest request) throws ServletException {
        NewProduct newProduct = new NewProduct();
        //newProduct.setPercentage(20);
        //productDao.saveProduct(newProduct);
        return newProduct;
    }
     */
    
    @Override
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
 
		Product product = new Product();
		//simpleProductManager.saveProduct(product);
		return product;
	}

    public void setProductManager(ProductManager productManager) {
        this.productManager = productManager;
    }

    public ProductManager getProductManager() {
        return productManager;
    }

}